package psn.kiko.controller.user;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import psn.kiko.annotation.api.StableID;
import psn.kiko.constant.MessageConstant;
import psn.kiko.context.UserContext;
import psn.kiko.pojo.PageResult;
import psn.kiko.pojo.Result;
import psn.kiko.pojo.dto.UserDTO;
import psn.kiko.pojo.dto.UserLoginDTO;
import psn.kiko.pojo.entity.system.UserEntity;
import psn.kiko.pojo.vo.AllocateRoleVo;
import psn.kiko.pojo.vo.DelDetailVO;
import psn.kiko.pojo.vo.LabelValue;
import psn.kiko.pojo.vo.UserLoginVO;
import psn.kiko.service.business.system.UserService;

import java.util.List;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/admin/user")
@Api(tags = {"用户管理"})
@Slf4j
public class UserController{
	@Autowired
	private UserService userService;
	
	@PostMapping("/login")
	@ApiOperation("用户登录")
	@StableID(1)
	public Result<?> login(@RequestBody @ApiParam("用户登录DTO") UserLoginDTO userLoginDTO){
		UserLoginVO userLoginVO = userService.loginByPasswordAndName(userLoginDTO);
		return Result.success(MessageConstant.USER_LOGIN_SUCCESS,userLoginVO);
	}
	
	/*@GetMapping("/getAviliableOperations")
	@ApiOperation("获取当前用户的可用操作")*/
	
	@PutMapping("/logout")
	@ApiOperation("用户退出登录")
	@StableID(2)
	public Result logout(){
		Integer r = userService.logoutById(UserContext.getUserId());
		return Result.success(r);
	}
	
	@GetMapping("/allAllocatedApisOfMine/{userId}")
	@ApiOperation("获取当前用户可以访问的所有API")
	@StableID(3)
	public Result allAllocatedApisOfMine(@PathVariable("userId") @ApiParam("userId") Integer userId){
		List apisOfUser = userService.getAllAllocatedApisOfUser(userId);
		return Result.success(apisOfUser);
	}
	
	@PostMapping("/register")
	@ApiOperation("用户注册")
	@StableID(4)
	public Result register(@RequestBody @ApiParam("用户DTO") UserDTO userDTO){
		Integer r = userService.addUser(userDTO);
		return Result.success(r);
	}
	
	@PostMapping("/addUser")
	@ApiOperation("增加用户")
	@StableID(5)
	public Result addUser(@RequestBody @ApiParam("用户DTO") UserDTO userDTO){
		Integer r = userService.addUser(userDTO);
		return Result.success(r);
	}
	
	@PostMapping("/batchAddUsers")
	@ApiOperation("批量增加用户")
	@StableID(6)
	public Result batchAddUsers(@RequestBody @ApiParam("用户DTO集合") List<UserDTO> userDTOs){
		Integer r = userService.addUsers(userDTOs);
		return Result.success();
	}
	
	@DeleteMapping("/batchDeleteByIds")
	@ApiOperation("根据主键批量删除用户")
	@StableID(7)
	public Result batchDeleteByIds(@RequestBody @ApiParam("用户id集合") List<Integer> ids){
		List<DelDetailVO> delDetailVOS = userService.deleteUsersByIds(ids);
		return Result.success(delDetailVOS);
	}
	
	@PostMapping("/pagingGetWithCondition/{pageNum}/{pageSize}")
	@ApiOperation("携带条件地分页获取用户")
	@StableID(8)
	public Result pagingGetWithCondition(
			@PathVariable("pageNum") @ApiParam("页码") int pageNum,
			@PathVariable("pageSize") @ApiParam("每页大小") int pageSize,
			@RequestBody @ApiParam("用户DTO") UserDTO userDTO){
		PageResult pageResult = userService.selectUsersByPage(userDTO,pageNum,pageSize);
		return Result.success(pageResult);
	}
	
	@PutMapping("batchEnableByIds")
	@ApiOperation("根据主键批量启用用户")
	@StableID(9)
	public Result batchEnableByIds(@RequestBody @ApiParam("用户id集合") List<Integer> ids){
		Integer r = userService.enableUsersByIds(ids);
		return Result.success(r);
	}
	
	@PutMapping("batchDisableByIds")
	@ApiOperation("根据主键批量禁用用户")
	@StableID(10)
	public Result batchDisableByIds(@RequestBody @ApiParam("用户id集合") List<Integer> ids){
		Integer r = userService.disableUsersByIds(ids);
		return Result.success(r);
	}
	
	@GetMapping("/{userId}")
	@ApiOperation("根据主键获取用户")
	@StableID(11)
	public Result getUserById(@PathVariable("userId") @ApiParam("用户主键") Integer userId){
		UserEntity userEntity = userService.getUserById(userId);
		return Result.success(userEntity);
	}
	
	@PutMapping("/edit")
	@ApiOperation("编辑用户")
	@StableID(12)
	public Result editUser(@RequestBody @ApiParam("用户DTO") UserDTO userDTO){
		Integer r = userService.updateUser(userDTO);
		return Result.success(r);
	}
	
	@GetMapping("/allocatedRoles/{userId}")
	@ApiOperation("获取用户已分配的角色")
	@StableID(13)
	public Result allocatedRoles(@PathVariable("userId") @ApiParam("用户id") Integer userId){
		List<AllocateRoleVo> allocateRoles = userService.getAllocatedRolesByUserId(userId);
		return Result.success(allocateRoles);
	}
	
	@GetMapping("/unAllocatedRoles/{userId}")
	@ApiOperation("获取用户未分配到的角色")
	@StableID(14)
	public Result unAllocatedRoles(@PathVariable("userId") @ApiParam("用户id") Integer userId){
		List<AllocateRoleVo> unallocatedRoles = userService.getUnallocatedRolesByUserId(userId);
		return Result.success(unallocatedRoles);
	}
	
	@PostMapping("/batchAllocateRoles/{userId}")
	@ApiOperation("分配给用户一批角色")
	@StableID(15)
	public Result batchAllocateRoles(
			@PathVariable("userId") @ApiParam("用户id") Integer userId,
			@RequestBody @ApiParam("角色主键集合") List<Integer> roleIds){
		Integer r = userService.batchAllocateRolesToUser(roleIds,userId);
		return Result.success(r);
	}
	
	@PostMapping("/batchUnAllocateRoles/{userId}")
	@ApiOperation("删除用户的一批角色")
	@StableID(16)
	public Result batchUnAllocateRoles(
			@PathVariable("userId") @ApiParam("用户id") Integer userId,
			@RequestBody @ApiParam("角色主键集合") List<Integer> roleIds){
		Integer r = userService.batchUnAllocateRolesToUser(roleIds,userId);
		return Result.success(r);
	}
	
	@PutMapping("/batchEnableRolesOfUser/{userId}")
	@ApiOperation("启用用户的一批角色")
	@StableID(17)
	public Result enableRolesOfUser(
			@PathVariable("userId") @ApiParam("用户id") Integer userId,
			@RequestBody @ApiParam("角色主键集合") List<Integer> roleIds){
		Integer r = userService.enableRolesOfUser(roleIds,userId);
		return Result.success(r);
	}
	
	@PutMapping("/batchDisableRolesOfUser/{userId}")
	@ApiOperation("禁用用户的一批角色")
	@StableID(18)
	public Result disableRolesOfUser(
			@PathVariable("userId") @ApiParam("用户id") Integer userId,
			@RequestBody @ApiParam("角色主键集合") List<Integer> roleIds){
		Integer r = userService.disableRolesOfUser(roleIds,userId);
		return Result.success(r);
	}
	
	@GetMapping("/allGenders")
	@ApiOperation("获取所有携带说明的性别")
	@StableID(19)
	public Result allGenders(){
		List<LabelValue> gendersWithLabel = userService.getAllGenders();
		return Result.success(gendersWithLabel);
	}
	
	@GetMapping("/allStatus")
	@ApiOperation("获取携带说明的状态所有可选值")
	@StableID(20)
	public Result<?> allStatus(){
		List<LabelValue> allStatus = userService.getStatusesWithLabel();
		return Result.success(allStatus);
	}
}
